﻿<?php
// download all package from server in own server path - set user:pw@ and download_dir - 09.2010
// powerd by Max http://pastebin.de/9696

global $servers;
global $serverUser;
	$servers = array(
			'update.deixu.net',
			'community4wcf.eu/filebase/packages/',
			'community4wcf.eu/filebase/packages/testing/',
			'community4wcf.eu/filebase/packages/unstable/',
			'community.woltlab.com/db/packages',
			'community.woltlab.com/db/packages/tempest'
	);
	
	$serverUser = array(
			'update.deixu.net' => '',
			'community4wcf.eu/filebase/packages/' => 'user:pw@',
			'community4wcf.eu/filebase/packages/testing/' => 'user:pw@',
			'community4wcf.eu/filebase/packages/unstable/' => 'user:pw@',
			'community.woltlab.com/db/packages' => 'user:pw@',
			'community.woltlab.com/db/packages/tempest' => 'user:pw@'
	);

class down {

	public function __construct() {
		global $servers;
		global $download_dir_arr;
		$download_dir_arr = array();
		foreach($servers as $server) {
			$dserver = explode("/", $server);
			$download_dir = __DIR__ . "/downloads/".$server;
			$download_dir_arr[] = $download_dir;
			if(!is_dir($download_dir)) { mkdir($download_dir, 0777, true); chmod($download_dir, 0777); }
		}
	}
	
	function flush(){
	    echo(str_repeat(' ',256));
	    // check that buffer is actually set before flushing
	    if (ob_get_length()){           
	        @ob_flush();
	        @flush();
        	@ob_end_flush();
	    }   
	    @ob_start();
	}

	function download($server, $download_dir) {
		global $serverUser;
		echo "<h1>" . $server . "</h1>";
		$this->flush();
		$baseurl = "http://".$serverUser[$server].$server."/";
			$xml = simplexml_load_file($baseurl);
		foreach($xml->package as $package) {
		foreach($package->versions->version as $version) {
			if($package->versions->version->file == "") {
				$newlink = $baseurl . "?packageName=" . $package['name'] . "&version=" . $version['name'];
				if($data = @fopen($newlink,'r')) {
					$content = stream_get_contents($data);
					fclose($data);
					$data = fopen($download_dir."/".$package['name']."_".$version['name'].".tar",'w');
					fwrite($data, $content);
					fclose($data);
					$this->flush();
				}
			} else {
				if($data = @fopen($package->versions->version->file,'r')) {
					$content = stream_get_contents($data);
					fclose($data);
					$data = fopen($download_dir."/".$package['name']."_".$version['name'].".tar",'w');
					fwrite($data, $content);
					fclose($data);
					$this->flush();
				}
			}
		}
		}
	echo "<h2>Finished</h2>";
	return true;
	}
}

$down = new down();
$i = 0;
global $download_dir_arr;
foreach($servers as $server) {
	if(!$down->download($server, $download_dir_arr[$i]) == true) die('ERROR');
	$i++;
}
?>